/**
 * BCButton
 * 对原生button进行扩展
 * 禁用时可选自动置灰
 * 可以增加点击音效
 */

import { _decorator, Component, Node, Button, AudioClip, Sprite, EventTouch } from 'cc';
import { BCAudioMgr } from '../../controller/BCAudioMgr';
const { ccclass, property } = _decorator;

@ccclass('BCButton')
export class BCButton extends Button {
	@property(AudioClip)
	private m_clickClip: AudioClip = null;

	@property({ displayName: "Grayscale", tooltip: "按钮禁用时是否置灰" })
	private m_grayScale: boolean = false;

	set interactable(value: boolean) {
		this._interactable = value;
		if (this.m_grayScale) {
			const img = this.getComponent(Sprite);
			img && (img.grayscale = !value)
		}
	}

	get interactable(): boolean {
		return super.interactable;
	}

	protected _zoomScale: number = 1.1;
	_onTouchBegan = function (event: EventTouch) {
		if (!this._interactable || !this.enabledInHierarchy) {
			return;
		}
		this._pressed = true;
		this._updateState();
		if (event) {
			event.propagationStopped = true;
		}
		BCAudioMgr.instance.playSound(this.m_clickClip);
	}
}


